package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgConsultingServiceMapper;
import com.ztgf.shopping.models.bo.epg.EpgConsultingServiceQueryBO;
import com.ztgf.shopping.models.bo.epg.EpgConsultingServiceSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgConsultingServiceDTO;
import com.ztgf.shopping.models.service.epg.IEpgConsultingService;
import com.ztgf.shopping.models.vo.epg.EpgConsultingServicePageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@DubboService
public class EpgConsultingServiceImpl extends ServiceImpl<EpgConsultingServiceMapper, EpgConsultingServiceDTO> implements IEpgConsultingService {


    @Resource
    private YmlConfig ymlConfig;


    @Override
    public Result<?> add(EpgConsultingServiceSaveBO bo) {
        Long id = ymlConfig.getPkId();
        EpgConsultingServiceDTO insertDto = new EpgConsultingServiceDTO();
        insertDto.setId(id);
        insertDto.setCreateUserId(bo.getSysUserId());
        if (CustomizeUtil.strIsNotNull(bo.getTitles())) {
            insertDto.setTitles(bo.getTitles());
        }
        if (CustomizeUtil.strIsNotNull(bo.getContent())) {
            insertDto.setContent(bo.getContent());
        }
        insertDto.setStatus(0);
        insertDto.setIsTop(bo.getIsTop());
        insertDto.setIsPublish(1);
        // 获取最大排序号
        Integer maxSort = baseMapper.getMaxSort();
        insertDto.setSort(maxSort + 1);
        insertDto.setIsHot(bo.getIsHot());
        boolean insert = this.insert(insertDto);
        if (!insert) {
            return new Result<>(CodeEnum.FAIL_100007);
        }
        return new Result<>(CodeEnum.SUCCESS);
    }

    @Override
    public Page<EpgConsultingServicePageVO> queryPageList(EpgConsultingServiceQueryBO queryBO) {
        Page<EpgConsultingServicePageVO> page = new Page<>(queryBO.getPageNo(), queryBO.getPageSize());
        List<EpgConsultingServicePageVO> list = baseMapper.queryPageList(page, queryBO);
        if (CustomizeUtil.listIsNotNull(list)) {
            page.setRecords(list);
        }
        return page;
    }

    @Override
    public Result<?> update(EpgConsultingServiceSaveBO updateBo) {
        EpgConsultingServiceDTO dto = this.selectById(updateBo.getId());
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_COMMODITY_ERROR_340000);
        }
        EpgConsultingServiceDTO updateDto = new EpgConsultingServiceDTO();
        updateDto.setId(dto.getId());
        updateDto.setUpdateUserId(updateBo.getSysUserId());
        if (CustomizeUtil.strIsNotNull(updateBo.getTitles())) {
            updateDto.setTitles(updateBo.getTitles());
        }
        if (CustomizeUtil.strIsNotNull(updateBo.getContent())) {
            updateDto.setContent(updateBo.getContent());
        }
        updateDto.setIsTop(updateBo.getIsTop());
        updateDto.setIsHot(updateBo.getIsHot());
        updateDto.setUpdateTime(new Date());
        boolean update = this.updateById(updateDto);
        if (!update) {
            return new Result<>(CodeEnum.FAIL_100008);
        }
        return new Result<>(CodeEnum.SUCCESS);
    }

    @Override
    public Result<?> delete(Long id) {
        EpgConsultingServiceDTO updateDto = new EpgConsultingServiceDTO();
        updateDto.setId(id);
        updateDto.setStatus(1);
        updateDto.setUpdateTime(new Date());
        boolean update = this.updateById(updateDto);
        if (!update) {
            return new Result<>(CodeEnum.FAIL_100009);
        }
        return new Result<>(CodeEnum.SUCCESS);
    }

    @Override
    public Result<?> updateIsTop(Long id) {
        EpgConsultingServiceDTO dto = this.selectById(id);
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_COMMODITY_ERROR_340000);
        }
        // 如果是就取消置顶
        EpgConsultingServiceDTO updateDto = new EpgConsultingServiceDTO();
        updateDto.setId(dto.getId());
        if (dto.getIsTop().equals(1)) {
            updateDto.setIsTop(0);
        } else {
            updateDto.setIsTop(1);
        }
        updateDto.setUpdateTime(new Date());
        boolean update = this.updateById(updateDto);
        if (!update) {
            return new Result<>(CodeEnum.FAIL);
        }
        return new Result<>(CodeEnum.SUCCESS);
    }

    @Override
    public Result<?> updateIsHot(Long id) {
        EpgConsultingServiceDTO dto = this.selectById(id);
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_COMMODITY_ERROR_340000);
        }
        EpgConsultingServiceDTO updateDto = new EpgConsultingServiceDTO();
        updateDto.setId(dto.getId());
        if (dto.getIsHot().equals(0)) {
            updateDto.setIsHot(1);
        } else {
            updateDto.setIsHot(0);
        }
        updateDto.setUpdateTime(new Date());
        boolean update = this.updateById(updateDto);
        if (!update) {
            return new Result<>(CodeEnum.FAIL);
        }
        return new Result<>(CodeEnum.SUCCESS);
    }

    @Override
    public Result<?> updateSort(Long id, Integer sort) {
        // sort:0上，1下
        EpgConsultingServiceDTO dto = this.selectById(id);
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_COMMODITY_ERROR_340000);
        }
        // 原来的序号
        Integer dtoSort = dto.getSort();
        // 要修改序号
        Integer number;
        if (sort.equals(0)) {
            // 判断序号是否为最小排序号
            Integer minSort = baseMapper.getMinSort();
            if (dtoSort.equals(minSort)) {
                return new Result<>(CodeEnum.SORT_FAIL);
            }
            // 获取比当前序号小一位的数据
            Wrapper<EpgConsultingServiceDTO> smallWrapper = new EntityWrapper<>();
            smallWrapper.eq("status", 0);
            smallWrapper.lt("sort", dtoSort);
            smallWrapper.orderBy("sort", false);
            EpgConsultingServiceDTO smallDto = this.selectOne(smallWrapper);
            number = smallDto.getSort();
            EpgConsultingServiceDTO smallUpdateDto = new EpgConsultingServiceDTO();
            smallUpdateDto.setId(smallDto.getId());
            smallUpdateDto.setSort(dtoSort);
            smallUpdateDto.setUpdateTime(new Date());
            this.updateById(smallUpdateDto);
        } else {
            // 判断是否为最大排序号
            Integer maxSort = baseMapper.getMaxSort();
            if (dtoSort.equals(maxSort)) {
                return new Result<>(CodeEnum.SORT_FAIL_A);
            }
            // 获取比当前序号大一位的数据
            Wrapper<EpgConsultingServiceDTO> maxWrapper = new EntityWrapper<>();
            maxWrapper.eq("status", 0);
            maxWrapper.gt("sort", dtoSort);
            maxWrapper.orderBy("sort", true);
            EpgConsultingServiceDTO maxDto = this.selectOne(maxWrapper);
            number = maxDto.getSort();
            EpgConsultingServiceDTO maxUpdateDto = new EpgConsultingServiceDTO();
            maxUpdateDto.setId(maxDto.getId());
            maxUpdateDto.setSort(dtoSort);
            maxUpdateDto.setUpdateTime(new Date());
            this.updateById(maxUpdateDto);
        }
        EpgConsultingServiceDTO updateDto = new EpgConsultingServiceDTO();
        updateDto.setId(dto.getId());
        updateDto.setSort(number);
        updateDto.setUpdateTime(new Date());
        boolean update = this.updateById(updateDto);
        if (!update) {
            return new Result<>(CodeEnum.FAIL);
        }
        return new Result<>(CodeEnum.SUCCESS);
    }

    @Override
    public Result<?> deleteBatch(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            this.delete(Long.valueOf(id));
        }
        return new Result<>(CodeEnum.SUCCESS);
    }

    @Override
    public boolean updateIsPush(Long id) {
        EpgConsultingServiceDTO dto = this.selectById(id);
        if (dto == null) {
            return false;
        }
        EpgConsultingServiceDTO updateDto = new EpgConsultingServiceDTO();
        updateDto.setId(dto.getId());
        if (dto.getIsPublish().equals(1)) {
            updateDto.setIsPublish(0);
        } else {
            updateDto.setIsPublish(1);
        }
        updateDto.setUpdateTime(new Date());
        return this.updateById(updateDto);
    }

}
